#include "audioform.h"
#include "ui_audioform.h"
#include <QKeyEvent>
#include <QSerialPort>
#include <QDebug>
#include "uart.h"
#define MAX_VOLUME  10
#ifdef Q_PROCESSOR_X86
#define PIC_RR  ".//image//cc//RR.jpg"
#define PIC_RL  ".//image//cc//RL.jpg"
#define PIC_FR  ".//image//cc//FR.jpg"
#define PIC_FL  ".//image//cc//FL.jpg"
#define PIC_RLC  ".//image//cc//RLC.jpg"
#define PIC_RRC  ".//image//cc//RRC.jpg"
#define PIC_FLC  ".//image//cc//FLC.jpg"
#define PIC_FRC  ".//image//cc//FRC.jpg"
#define PIC_LFE  ".//image//cc//LFE.jpg"
#define PIC_FC  ".//image//cc//FC.jpg"
#define PIC_RC  ".//image//cc//RC.jpg"
#define PIC_Center  ".//image//cc//Center.jpg"
#else
#define PIC_RR  "//usr//bin//image//cc//RR.jpg"
#define PIC_RL  "//usr//bin//image//cc//RL.jpg"
#define PIC_FR  "//usr//bin//image//cc//FR.jpg"
#define PIC_FL  "//usr//bin//image//cc//FL.jpg"
#define PIC_RLC  "//usr//bin//image//cc//RLC.jpg"
#define PIC_RRC  "//usr//bin//image//cc//RRC.jpg"
#define PIC_FLC  "//usr//bin//image//cc//FLC.jpg"
#define PIC_FRC  "//usr//bin//image//cc//FRC.jpg"
#define PIC_LFE  "//usr//bin//image//cc//LFE.jpg"
#define PIC_FC  "//usr//bin//image//cc//FC.jpg"
#define PIC_RC  "//usr//bin//image//cc//RC.jpg"
#define PIC_Center  "//usr//bin//image//cc//Center.jpg"
#endif
enum
{
    au_Sampling_Rate=0,
    au_Bit_Depth,
    au_Sine_Wave_Tone,
    au_Channel_Config,
    au_Volume
};
audioForm::audioForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::audioForm)
{
    ui->setupUi(this);
    this->setStyleSheet("background-color: rgb(0, 186, 238);");
    current_item=-1;
    current_volume=0;
    ui->asr_widget->hide();ui->checkBox0->setFocus();
    ui->abd_widget->hide();
    ui->av_widget->hide();
    ui->acc_widget->hide();
    ui->swt_widget->hide();
    ui->progressBar->setMinimum(0);
    ui->progressBar->setMaximum(MAX_VOLUME);
    box_sel[au_Sampling_Rate]=NULL;
    box_sel[au_Bit_Depth]=NULL;
    box_sel[au_Sine_Wave_Tone]=NULL;
    box_sel[au_Channel_Config]=NULL;
    box_sel[au_Volume]=NULL;
    {
        QImage* img=new QImage;
        if(! ( img->load(PIC_RR) ) ) //加载图像
        {
            delete img;
            return;
        }
        ui->label_RR->setPixmap(QPixmap::fromImage(*img));
        if(! ( img->load(PIC_RL) ) ) //加载图像
        {
            delete img;
            return;
        }
        ui->label_RL->setPixmap(QPixmap::fromImage(*img));
        if(! ( img->load(PIC_FR) ) ) //加载图像
        {
            delete img;
            return;
        }
        ui->label_FR->setPixmap(QPixmap::fromImage(*img));
        if(! ( img->load(PIC_FL) ) ) //加载图像
        {
            delete img;
            return;
        }
        ui->label_FL->setPixmap(QPixmap::fromImage(*img));
        if(! ( img->load(PIC_RRC) ) ) //加载图像
        {
            delete img;
            return;
        }
        ui->label_RRC->setPixmap(QPixmap::fromImage(*img));
        if(! ( img->load(PIC_RLC) ) ) //加载图像
        {
            delete img;
            return;
        }
        ui->label_RLC->setPixmap(QPixmap::fromImage(*img));
        if(! ( img->load(PIC_FRC) ) ) //加载图像
        {
            delete img;
            return;
        }
        ui->label_FRC->setPixmap(QPixmap::fromImage(*img));
        if(! ( img->load(PIC_FLC) ) ) //加载图像
        {
            delete img;
            return;
        }
        ui->label_FLC->setPixmap(QPixmap::fromImage(*img));
        if(! ( img->load(PIC_LFE) ) ) //加载图像
        {
            delete img;
            return;
        }
        ui->label_LFE->setPixmap(QPixmap::fromImage(*img));
        if(! ( img->load(PIC_FC) ) ) //加载图像
        {
            delete img;
            return;
        }
        ui->label_FC->setPixmap(QPixmap::fromImage(*img));
        if(! ( img->load(PIC_RC) ) ) //加载图像
        {
            delete img;
            return;
        }
        ui->label_RC->setPixmap(QPixmap::fromImage(*img));
        if(! ( img->load(PIC_Center) ) ) //加载图像
        {
            delete img;
            return;
        }
        ui->label_Center->setPixmap(QPixmap::fromImage(*img));
        img=GetLogo();
        if(img!=NULL)
            ui->label_Logo->setPixmap(QPixmap::fromImage(*img).scaled(ui->label_Logo->size()));

    }
}
void audioForm::select_box(int nConfig,QCheckBox *sel)
{
    if(box_sel[nConfig]!=NULL&&box_sel[nConfig]!=sel)
        box_sel[nConfig]->setChecked(false);
    else
        sel->setChecked(true);
    box_sel[nConfig]=sel;
}

void audioForm::Show_Speaker(int nChannelConfig)
{
    bool bFR_FL,bLFE,bFC,bRC,bRR_RL,bRRC_RLC,bFRC_FLC;
    bFR_FL=true;
    bLFE=(nChannelConfig%2)?true:false;
    bFC=(nChannelConfig%4>1)?true:false;
    bRC=(nChannelConfig>=4&&nChannelConfig<8)||(nChannelConfig>=0x0c&&nChannelConfig<0x10)||(nChannelConfig>=0x18&&nChannelConfig<0x1c);
    bRR_RL=(nChannelConfig>=8&&nChannelConfig<0x14)||(nChannelConfig>=0x1c&&nChannelConfig<=0x1f);
    bRRC_RLC=(nChannelConfig>=0x10&&nChannelConfig<0x14);
    bFRC_FLC=(nChannelConfig>=0x14&&nChannelConfig<=0x1f);
    ui->label_FC->setVisible(bFC);
    ui->label_FL->setVisible(bFR_FL);
    ui->label_FR->setVisible(bFR_FL);
    ui->label_LFE->setVisible(bLFE);
    ui->label_RL->setVisible(bRR_RL);
    ui->label_RR->setVisible(bRR_RL);
    ui->label_RLC->setVisible(bRRC_RLC);
    ui->label_RRC->setVisible(bRRC_RLC);
    ui->label_FLC->setVisible(bFRC_FLC);
    ui->label_FRC->setVisible(bFRC_FLC);
    ui->label_RC->setVisible(bRC);
}

void audioForm::showMenu(int item)
{
 //   if (item==current_item)
   //     return;
    switch (current_item)
    {
    case au_Sampling_Rate: ui->asr_widget->hide();break;
    case au_Bit_Depth: ui->abd_widget->hide();break;
    case au_Sine_Wave_Tone: ui->swt_widget->hide();break;
    case au_Volume: ui->av_widget->hide();break;
    case au_Channel_Config: ui->acc_widget->hide();break;
    }
    switch(item)
    {
    case au_Sampling_Rate:
        ui->asr_widget->show();
        ui->checkBox->setFocus();
        break;
    case au_Bit_Depth:
        ui->abd_widget->show();
        ui->checkBox_9->setFocus();
        break;
    case au_Sine_Wave_Tone:
        ui->swt_widget->show();
        ui->checkBox_11->setFocus();
        break;
    case au_Volume:
        ui->av_widget->show();
        ui->progressBar->setFocus();
        break;
    case au_Channel_Config:
        ui->acc_widget->show();
        ui->checkBox_25->setFocus();
        break;
    }
    current_item=item;
    return;
}

audioForm::~audioForm()
{
    delete ui;
}

void audioForm::on_checkBox0_clicked()
{
    showMenu(au_Sampling_Rate);
    select_box(au_Volume,ui->checkBox0);
}

void audioForm::on_checkBox0_2_clicked()
{
    showMenu(au_Bit_Depth);
    select_box(au_Volume,ui->checkBox0_2);
}

void audioForm::on_checkBox0_3_clicked()
{
    showMenu(au_Sine_Wave_Tone);
    select_box(au_Volume,ui->checkBox0_3);
}

void audioForm::on_checkBox0_4_clicked()
{
    showMenu(au_Volume);
    select_box(au_Volume,ui->checkBox0_4);
}

void audioForm::on_checkBox0_5_clicked()
{
    showMenu(au_Channel_Config);
    select_box(au_Volume,ui->checkBox0_5);
}

void audioForm::keyPressEvent(QKeyEvent *event)
{
    int key=event->key();
    if(current_item==au_Volume)
    {
        if(key==Qt::Key_Right||key==Qt::Key_Up)
        {
            if(current_volume<MAX_VOLUME)
            {
                ui->progressBar->setValue(++current_volume);
                sendUartCmd(audio_volume_setup,current_volume);
            }

        }
        else if(key==Qt::Key_Left||key==Qt::Key_Down)
        {
            if(current_volume>0)
             {
                ui->progressBar->setValue(--current_volume);
                sendUartCmd(audio_volume_setup,current_volume);
            }
        }
        if(key==Qt::Key_Space)
            ui->checkBox0_4->setFocus();
//        qDebug()<<current_volume;
    }
    if(key==Qt::Key_F12||key==Qt::Key_Exit)
    {
        if(ui->checkBox0->hasFocus()||ui->checkBox0_5->hasFocus()||ui->checkBox0_2->hasFocus()||ui->checkBox0_3->hasFocus()||ui->checkBox0_4->hasFocus())
        {
            if(ShowPreWin())
                this->hide();
        }
        else
            switch(current_item)
            {
            case au_Sampling_Rate:
                ui->asr_widget->hide();
                ui->checkBox0->setFocus();
                break;
            case au_Bit_Depth:
                ui->abd_widget->hide();
                ui->checkBox0_2->setFocus();
                break;
            case au_Sine_Wave_Tone:
                ui->swt_widget->hide();
                ui->checkBox0_3->setFocus();
                break;
            case au_Volume:
                ui->av_widget->hide();
                ui->checkBox0_4->setFocus();
                break;
            case au_Channel_Config:
                ui->acc_widget->hide();
                ui->checkBox0_5->setFocus();
                break;
            }
        //focusNextPrevChild(true);
    }
    else
    {
        qDebug()<<key;
        if(ShowWin(key,0))
            this->hide();
    }

}

void audioForm::on_checkBox_clicked()
{
    sendUartCmd(audio_sampling_rate_sel,audio_32K);
    select_box(au_Sampling_Rate,ui->checkBox);
}

void audioForm::on_checkBox_2_clicked()
{
    sendUartCmd(audio_sampling_rate_sel,audio_44_1K);
    select_box(au_Sampling_Rate,ui->checkBox_2);
}

void audioForm::on_checkBox_3_clicked()
{
    sendUartCmd(audio_sampling_rate_sel,audio_48K);
    select_box(au_Sampling_Rate,ui->checkBox_3);
}

void audioForm::on_checkBox_4_clicked()
{
    sendUartCmd(audio_sampling_rate_sel,audio_88K);
    select_box(au_Sampling_Rate,ui->checkBox_4);
}

void audioForm::on_checkBox_5_clicked()
{
    sendUartCmd(audio_sampling_rate_sel,audio_96K);
    select_box(au_Sampling_Rate,ui->checkBox_5);
}

void audioForm::on_checkBox_6_clicked()
{
    sendUartCmd(audio_sampling_rate_sel,audio_176K);
    select_box(au_Sampling_Rate,ui->checkBox_6);
}

void audioForm::on_checkBox_7_clicked()
{
    sendUartCmd(audio_sampling_rate_sel,audio_192K);
    select_box(au_Sampling_Rate,ui->checkBox_7);
}

void audioForm::on_checkBox_8_clicked()
{
    sendUartCmd(audio_bit_depth_sel,audio_16bit);
    select_box(au_Bit_Depth,ui->checkBox_8);
}

void audioForm::on_checkBox_9_clicked()
{
    sendUartCmd(audio_bit_depth_sel,audio_20bit);
    select_box(au_Bit_Depth,ui->checkBox_9);
}

void audioForm::on_checkBox_10_clicked()
{
    sendUartCmd(audio_bit_depth_sel,audio_24bit);
    select_box(au_Bit_Depth,ui->checkBox_10);
}

void audioForm::on_checkBox_11_clicked()
{
    sendUartCmd(sinewave_tone,PCM_SINEWAVE_100);
    select_box(au_Sine_Wave_Tone,ui->checkBox_11);
}

void audioForm::on_checkBox_12_clicked()
{
    sendUartCmd(sinewave_tone,PCM_SINEWAVE_200);
    select_box(au_Sine_Wave_Tone,ui->checkBox_12);
}

void audioForm::on_checkBox_13_clicked()
{
    sendUartCmd(sinewave_tone,PCM_SINEWAVE_300);
    select_box(au_Sine_Wave_Tone,ui->checkBox_13);
}

void audioForm::on_checkBox_14_clicked()
{
    sendUartCmd(sinewave_tone,PCM_SINEWAVE_400);
    select_box(au_Sine_Wave_Tone,ui->checkBox_14);
}

void audioForm::on_checkBox_15_clicked()
{
    sendUartCmd(sinewave_tone,PCM_SINEWAVE_500);
    select_box(au_Sine_Wave_Tone,ui->checkBox_15);
}

void audioForm::on_checkBox_16_clicked()
{
    sendUartCmd(sinewave_tone,PCM_SINEWAVE_700);
    select_box(au_Sine_Wave_Tone,ui->checkBox_16);
}

void audioForm::on_checkBox_17_clicked()
{
    sendUartCmd(sinewave_tone,PCM_SINEWAVE_1K);
    select_box(au_Sine_Wave_Tone,ui->checkBox_17);
}

void audioForm::on_checkBox_18_clicked()
{
    sendUartCmd(sinewave_tone,PCM_SINEWAVE_900);
    select_box(au_Sine_Wave_Tone,ui->checkBox_18);
}

void audioForm::on_checkBox_19_clicked()
{
    sendUartCmd(sinewave_tone,PCM_SINEWAVE_800);
    select_box(au_Sine_Wave_Tone,ui->checkBox_19);
}

void audioForm::on_checkBox_20_clicked()
{
    sendUartCmd(sinewave_tone,PCM_SINEWAVE_600);
    select_box(au_Sine_Wave_Tone,ui->checkBox_20);
}

void audioForm::on_checkBox_21_clicked()
{
    sendUartCmd(sinewave_tone,PCM_SINEWAVE_2K);
    select_box(au_Sine_Wave_Tone,ui->checkBox_21);
}

void audioForm::on_checkBox_22_clicked()
{
    sendUartCmd(sinewave_tone,PCM_SINEWAVE_3K);
    select_box(au_Sine_Wave_Tone,ui->checkBox_22);
}

void audioForm::on_checkBox_23_clicked()
{
    sendUartCmd(sinewave_tone,PCM_SINEWAVE_4K);
    select_box(au_Sine_Wave_Tone,ui->checkBox_23);
}

void audioForm::on_checkBox_24_clicked()
{
    sendUartCmd(sinewave_tone,PCM_SINEWAVE_5K);
    select_box(au_Sine_Wave_Tone,ui->checkBox_24);
}

void audioForm::on_checkBox_25_clicked()
{
    sendUartCmd(audio_speaker_placement,FR_FL);
    select_box(au_Channel_Config,ui->checkBox_25);
    Show_Speaker(FR_FL);
//    ui->label_FC->setVisible(false);
}

void audioForm::on_checkBox_26_clicked()
{
    sendUartCmd(audio_speaker_placement,LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_26);
    Show_Speaker(LFE_FR_FL);
}

void audioForm::on_checkBox_27_clicked()
{
    sendUartCmd(audio_speaker_placement,FC_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_27);
    Show_Speaker(FC_FR_FL);
}

void audioForm::on_checkBox_28_clicked()
{
    sendUartCmd(audio_speaker_placement,FC_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_28);
    Show_Speaker(FC_LFE_FR_FL);
}

void audioForm::on_checkBox_29_clicked()
{
    sendUartCmd(audio_speaker_placement,RC_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_29);
    Show_Speaker(RC_FR_FL);
}

void audioForm::on_checkBox_30_clicked()
{
    sendUartCmd(audio_speaker_placement,RC_FC_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_30);
    Show_Speaker(RC_FC_LFE_FR_FL);
}

void audioForm::on_checkBox_31_clicked()
{
    sendUartCmd(audio_speaker_placement,RC_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_31);
    Show_Speaker(RC_LFE_FR_FL);
}

void audioForm::on_checkBox_32_clicked()
{
    sendUartCmd(audio_speaker_placement,RC_FC_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_32);
    Show_Speaker(RC_FC_FR_FL);
}

void audioForm::on_checkBox_33_clicked()
{
    sendUartCmd(audio_speaker_placement,RR_RL_FC_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_33);
    Show_Speaker(RR_RL_FC_FR_FL);
}

void audioForm::on_checkBox_34_clicked()
{
    sendUartCmd(audio_speaker_placement,RR_RL_FC_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_34);
    Show_Speaker(RR_RL_FC_LFE_FR_FL);
}

void audioForm::on_checkBox_35_clicked()
{
    sendUartCmd(audio_speaker_placement,RC_RR_RL_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_35);
    Show_Speaker(RC_RR_RL_FR_FL);
}

void audioForm::on_checkBox_36_clicked()
{
    sendUartCmd(audio_speaker_placement,RC_RR_RL_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_36);
    Show_Speaker(RC_RR_RL_LFE_FR_FL);
}

void audioForm::on_checkBox_37_clicked()
{
    sendUartCmd(audio_speaker_placement,RC_RR_RL_FC_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_37);
    Show_Speaker(RC_RR_RL_FC_FR_FL);
}

void audioForm::on_checkBox_38_clicked()
{
    sendUartCmd(audio_speaker_placement,RC_RR_RL_FC_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_38);
    Show_Speaker(RC_RR_RL_FC_LFE_FR_FL);
}

void audioForm::on_checkBox_39_clicked()
{
    sendUartCmd(audio_speaker_placement,RRC_RLC_RR_RL_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_39);
    Show_Speaker(RRC_RLC_RR_RL_FR_FL);
}

void audioForm::on_checkBox_40_clicked()
{
    sendUartCmd(audio_speaker_placement,RRC_RLC_RR_RL_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_40);
    Show_Speaker(RRC_RLC_RR_RL_LFE_FR_FL);
}

void audioForm::on_checkBox_41_clicked()
{
    sendUartCmd(audio_speaker_placement,RRC_RLC_RR_RL_FC_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_41);
    Show_Speaker(RRC_RLC_RR_RL_FC_FR_FL);
}

void audioForm::on_checkBox_42_clicked()
{
    sendUartCmd(audio_speaker_placement,RRC_RLC_RR_RL_FC_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_42);
    Show_Speaker(RRC_RLC_RR_RL_FC_LFE_FR_FL);
}

void audioForm::on_checkBox_43_clicked()
{
    sendUartCmd(audio_speaker_placement,FRC_FLC_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_43);
    Show_Speaker(FRC_FLC_FR_FL);
}

void audioForm::on_checkBox_44_clicked()
{
    sendUartCmd(audio_speaker_placement,FRC_FLC_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_44);
    Show_Speaker(FRC_FLC_LFE_FR_FL);
}

void audioForm::on_checkBox_45_clicked()
{
    sendUartCmd(audio_speaker_placement,FRC_FLC_FC_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_45);
    Show_Speaker(FRC_FLC_FC_FR_FL);
}

void audioForm::on_checkBox_46_clicked()
{
    sendUartCmd(audio_speaker_placement,FRC_FLC_FC_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_46);
    Show_Speaker(FRC_FLC_FC_LFE_FR_FL);
}

void audioForm::on_checkBox_48_clicked()
{
    sendUartCmd(audio_speaker_placement,FRC_FLC_RC_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_48);
    Show_Speaker(FRC_FLC_RC_FR_FL);
}

void audioForm::on_checkBox_47_clicked()
{
    sendUartCmd(audio_speaker_placement,FRC_FLC_RC_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_47);
    Show_Speaker(FRC_FLC_RC_LFE_FR_FL);
}

void audioForm::on_checkBox_49_clicked()
{
    sendUartCmd(audio_speaker_placement,FRC_FLC_RC_FC_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_49);
    Show_Speaker(FRC_FLC_RC_FC_FR_FL);
}

void audioForm::on_checkBox_50_clicked()
{
    sendUartCmd(audio_speaker_placement,FRC_FLC_RC_FC_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_50);
    Show_Speaker(FRC_FLC_RC_FC_LFE_FR_FL);
}

void audioForm::on_checkBox_52_clicked()
{
    sendUartCmd(audio_speaker_placement,FRC_FLC_RR_RL_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_52);
    Show_Speaker(FRC_FLC_RR_RL_FR_FL);
}

void audioForm::on_checkBox_51_clicked()
{
    sendUartCmd(audio_speaker_placement,FRC_FLC_RR_RL_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_51);
    Show_Speaker(FRC_FLC_RR_RL_LFE_FR_FL);
}

void audioForm::on_checkBox_53_clicked()
{
    sendUartCmd(audio_speaker_placement,FRC_FLC_RR_RL_FC_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_53);
    Show_Speaker(FRC_FLC_RR_RL_FC_FR_FL);
}

void audioForm::on_checkBox_54_clicked()
{
    sendUartCmd(audio_speaker_placement,FRC_FLC_RR_RL_FC_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_54);
    Show_Speaker(FRC_FLC_RR_RL_FC_LFE_FR_FL);
}

void audioForm::on_checkBox_55_clicked()
{
    sendUartCmd(audio_speaker_placement,RR_RL_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_55);
    Show_Speaker(RR_RL_FR_FL);
}

void audioForm::on_checkBox_56_clicked()
{
    sendUartCmd(audio_speaker_placement,RR_RL_LFE_FR_FL);
    select_box(au_Channel_Config,ui->checkBox_56);
    Show_Speaker(RR_RL_LFE_FR_FL);
}
